package org.example.common.enums;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public enum EntityType {
    EVENT("EVENT", "事件"),
    LOCATION("LOC", "地点"),
    ORGANIZATION("ORG", "机构"),
    PERSON("PER", "人物"),
//    TIME("TIME", "时间"),
    UNKNOWN("", "未知");

    private final String prefix;
    private final String label;

    // 静态映射
    private static final Map<String, EntityType> PREFIX_MAP;

    static {
        PREFIX_MAP = Arrays.stream(values())
                .filter(e -> e != UNKNOWN)
                .collect(Collectors.toMap(
                        e -> e.prefix,
                        Function.identity()));
    }

    EntityType(String prefix, String label) {
        this.prefix = prefix;
        this.label = label;
    }

    public static EntityType fromKey(String key) {
        if (key == null) {
            return UNKNOWN;
        }

        for (Map.Entry<String, EntityType> entry : PREFIX_MAP.entrySet()) {
            if (key.startsWith(entry.getKey())) {
                return entry.getValue();
            }
        }
        return UNKNOWN;
    }

    public String getLabel() {
        return this.label;
    }
}